package com.pz.cloud.framework.workflow.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.business.society.entity.OrgSociety;
import com.pz.cloud.framework.business.society.entity.OrgSocietyUser;
import com.pz.cloud.framework.business.workflow.entity.*;
import com.pz.cloud.framework.business.workflow.service.ITaskNodeAttrService;
import com.pz.cloud.framework.business.workflow.service.ITaskNodeFieldService;
import com.pz.cloud.framework.business.workflow.service.ITaskNodeHandlerRuleService;
import com.pz.cloud.framework.util.BeanUpdateUtils;
import com.pz.cloud.framework.workflow.mapper.TaskNodeAttrMapper;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.task.Task;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;

/**
 * 任务节点配置
 * @author mazezhan
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class TaskNodeAttrServiceImpl extends ServiceImpl<TaskNodeAttrMapper, TaskNodeAttr> implements ITaskNodeAttrService {
    @Autowired
    private ITaskNodeHandlerRuleService taskNodeHandlerRuleService;
    @Autowired
    private ITaskNodeFieldService taskNodeFieldService;

    @Override
    public boolean save(TaskNodeAttr entity) {
        if(StringUtils.isEmpty(entity.getTaskKey())){
            throw new CustomRuntimeException("缺少参数:taskKey");
        }
        boolean flag ;
        TaskNodeAttr exisisEntity = super.getById(entity.getTaskKey());
        if (exisisEntity != null) {
            BeanUpdateUtils.copyNullProperties(entity, exisisEntity);
            flag = super.updateById(exisisEntity);
        }else{
            flag = super.save(entity);
        }
        List<TaskNodeHandlerRule> ruleList = entity.getHandlerRuleList();
        //先删除
        taskNodeHandlerRuleService.deleteByTaskKey(entity.getTaskKey());
        if(!CollectionUtils.isEmpty(ruleList)){
            for (TaskNodeHandlerRule rule : ruleList) {
                rule.setTaskKey(entity.getTaskKey());
            }
            taskNodeHandlerRuleService.saveOrUpdateBatch(ruleList);
        }

        taskNodeFieldService.deleteByTaskKey(entity.getTaskKey());
        List<TaskNodeField> fieldList = entity.getFieldList();
        if(!CollectionUtils.isEmpty(fieldList)){
            for (TaskNodeField field : fieldList) {
                field.setTaskKey(entity.getTaskKey());
            }
            taskNodeFieldService.saveOrUpdateBatch(fieldList);
        }

        return flag;
    }

    @Override
    public TaskNodeAttr getById(Serializable id) {
        TaskNodeAttr taskNodeAttr = super.getById(id);
        if(taskNodeAttr!=null){
            List<TaskNodeHandlerRule> ruleList = taskNodeHandlerRuleService.findByTaskKey(taskNodeAttr.getTaskKey());
            List<TaskNodeField> fieldList = taskNodeFieldService.findByTaskKey(taskNodeAttr.getTaskKey());
            taskNodeAttr.setHandlerRuleList(ruleList);
            taskNodeAttr.setFieldList(fieldList);
        }
        return taskNodeAttr;
    }
}
